using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using My.IdlConverter.Util;
using System.Configuration;

namespace My.IdlConverter.LoadManager
{
    public class LoadManagerInstance
    {
        public static Cfg.SearchPatterConfig DefaultConfig;
        private Cfg.SearchPatterConfig _config;
        private IConfigStrategy _configStrategy;

        static LoadManagerInstance()
        {
            DefaultConfig = new Cfg.SearchPatterConfig();
            DefaultConfig.RootPath = "";
            DefaultConfig.Scope = "";
            DefaultConfig.Pattern = "*.xcfg";
            DefaultConfig.Strategy = "My.IdlConverter.LoadManager.AddinConfigStrategy,IdlConverterMy.IdlConverter.LoadManager";
        }

        public LoadManagerInstance(string cfgSecion)
        {
            try
            {
                Cfg.SearchPatterConfig config = ConfigurationManager.GetSection(cfgSecion) as Cfg.SearchPatterConfig;
                if (config == null)
                    config = DefaultConfig;
                _config = config;
            }
            catch(System.Exception)
            {
                _config = DefaultConfig;
            }
        }

        public Cfg.SearchPatterConfig Config
        {
            get { return _config; }
        }

        public IConfigStrategy ConfigStrategy
        {
            get
            {
                if (_configStrategy == null)
                {
                    Type strategyType = ReflectHelper.ClassForName(_config.Strategy);
                    Type componentLoaderType = ReflectHelper.ClassForName(_config.ComponentLoader);

                    IComponentLoader componentLoader = Activator.CreateInstance(componentLoaderType) as IComponentLoader;
                    componentLoader.LoadManager = this;

                    IConfigStrategy configStrategy = Activator.CreateInstance(strategyType) as IConfigStrategy;
                    configStrategy.ComponentLoader = componentLoader;

                    _configStrategy = configStrategy;
                }

                return _configStrategy;

            }
        }

        public IList<UserComponent> Targets
        {
            get
            {
                IList<UserComponent> components = new List<UserComponent>();
                StringCollection addinCfgFiles = IOHelper.GetFiles(SearchScope, null, _config.Recursive, Config.Pattern);
                foreach (string nextCfgFile in addinCfgFiles)
                {
                    ConfigStrategy.ComponentLoader.ComponentPath = nextCfgFile;
                    ArrayHelper.AddRange(components, ConfigStrategy.ComponentLoader.Components);
                }

                return components;
            }
        }

        public string SearchScope
        {
            get
            {
                string fullPathName = _config.RootPath;
                if (!Directory.Exists(fullPathName))
                    fullPathName = ReflectHelper.ResolvePathByAssembly(System.Reflection.Assembly.GetExecutingAssembly(), _config.Scope);
                if (!Directory.Exists(fullPathName))
                    fullPathName = ReflectHelper.ResolvePathByAssembly(System.Reflection.Assembly.GetExecutingAssembly(), "plugins");

                return fullPathName;
            }
        }
    }
}
